package com.undsf.ezhttp;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by Arathi on 2017/4/6.
 */
public abstract class HashUtils {
    public static String MD5(byte[] data) {
        return Hash(data, "MD5");
    }

    public static String SHA1(byte[] data) {
        return Hash(data, "SHA-1");
    }

    public static String Hash(byte[] data, String algorithm) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithm);
            md.update(data);
            return new BigInteger(1, md.digest()).toString(16);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String Hash(String s, String algorithm) {
        byte[] data = s.getBytes();
        return Hash(data, algorithm);
    }
}
